import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../model/my_store.dart';

import '../widget/commit/my_appbar.dart';
class SelectTheme extends StatefulWidget {
  SelectTheme({
    Key key,
    this.setTheme
  }):super(key: key);
  dynamic setTheme;
  _SelectThemeState createState() => _SelectThemeState();
}

class _SelectThemeState extends State<SelectTheme> {
  int currentTheme;
  @override
  void initState () {
    super.initState();
    if (MyStore().getData('THEME') != null) {
      currentTheme = MyStore().getData('THEME')['index'];
    } else {
      MyStore().setData('THEME', {'index': 0});
      currentTheme = 0;
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: PreferredSize(
        child: MyAppBar(
          brightness: true,
          title: '选择主题',
        ),
        preferredSize: Size.fromHeight(ScreenUtil().setWidth(80)),
      ),
      body: Container(
        child: GridView.builder(
          padding: EdgeInsets.all(ScreenUtil().setWidth(20)),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 8.0,
            mainAxisSpacing: 8.0,
            childAspectRatio: 1.8
          ),
          itemCount: MyStore().themeList.length,
          itemBuilder: (BuildContext context, index) {
            var _color = MyStore().themeList[index];
            return Stack(
              children: <Widget>[
                Container(
                  decoration: BoxDecoration(
                    color: _color,
                    borderRadius: BorderRadius.circular(5)
                  ),
                ),
                index == currentTheme ? 
                Container(
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(0, 0, 0, 0.6),
                    borderRadius: BorderRadius.circular(5)
                  ),
                  child: Center(
                    child: Icon(
                      IconData(0xe60c, fontFamily: 'iconfont'),
                      size: ScreenUtil().setWidth(120),
                      color: Colors.white,
                    ),
                  ),
                ):InkWell(
                  onTap: (){
                    widget.setTheme.setColor(_color);
                    MyStore().setData('THEME', {'index': index});
                    setState(() {currentTheme = index;});
                  },
                )
              ],
            );
          },
        ),
      ),
    );
  }
}